//
//  HotViewcontroller.m
//  YSBEmployee
//
//  Created by developer on 2021/11/30.
//  Copyright © 2021 ysb. All rights reserved.
//

#import "AppDelegate.h"
#import "YSBChatRoomWebSocketManager.h"
#import "YSBGoodOrderViewController.h"
#import "YSBHomeController.h"
#import "YSBAccountStatusViewController.h"
#import "YSBChildOrderDetailComplaintViewController.h"

#import "YSBPunchCardDetailViewController.h"

#import "YSBWebViewController.h"

#import "YSBAnnouncementScrollView.h"


@implementation AppDelegate

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    [self openFaceAction];
    [self openWebSocket];

}


- (void)openFaceAction {
    
    [self openFaceWithValue:YES];
    
}

- (void)faceStart {
    [self.locationManager startUpdatingLocation];
    
}


- (void)faceStop {
    [self.locationManager stopUpdatingLocation];
    
}


@end



@implementation YSBChatRoomWebSocketManager

//判断当前类再进行处理
- (BOOL)judgeClassToDoAction {
    if ([[YSBCommonTool getCurrentVC] isKindOfClass:[YSBGoodOrderViewController class]]) {
        return YES;
    }else{
        return NO;
    }
}

- (void)judgeShowAlertWithModel:(YSBWebSocketPushMsgModel *)model {
    
    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    NSString *log_id = [localSave objectForKey:@"logIdKey"];

    //如果本地已保存出圈的记录id,则不弹出
    if (log_id.integerValue == model.log_id.integerValue && model.log_id.integerValue != 0) {
        NSLog(@"进入了相同不弹窗2222");

    }else {

        NSLog(@"进入了不同弹窗2222");

        //暂时不弹框
//        NSString *title = [YSBCommonTool isBlankString:model.title] ? @"温馨提示" :  model.title;
//        NSString *message = [YSBCommonTool isBlankString:model.content] ? @"" : model.content;
//
//        [YSBCommonTool showAlertViewControllerWithTitle:title andMessage:message andLeftBtnTitle:@"未确认" andRightBtnTitle:@"已确认" andLeftBtnAction:^(UIAlertAction * _Nonnull action) {
//            [[InterfaceSwitchingLogic defaultShareManager] uploadEnclosureConfirmWithlogID:model.log_id andStatus:@"2"];
//        } andRightBtnAction:^(UIAlertAction * _Nonnull action) {
//            [[InterfaceSwitchingLogic defaultShareManager] uploadEnclosureConfirmWithlogID:model.log_id andStatus:@"1"];
//        }];
//
//        if (model.log_id.integerValue > 0) {
//            [localSave setObject:model.log_id forKey:@"logIdKey"];
//            [localSave synchronize];//数据同步,写入文件
//        }

    }
    
}

//接收消息
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
    NSLog(@"接收消息 ---- %@", message);
    
    if ([message isKindOfClass:[NSString class]]) {

        NSDictionary *dict = [YSBCommonTool dictionaryWithJsonString:message];
        if (![YSBCommonTool isNullToDictionary:dict]) {
            
            if (![YSBCommonTool isBlankString:dict[@"action"]]) {
                NSNumber *action = dict[@"action"];
                
                if (action.intValue == 5) {//action为5的时候是推送消息
                    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
                    NSNumber *isOpenFenceNum = [localSave objectForKey:@"isOpenFaceKey"];
                    BOOL isOpenFence = isOpenFenceNum.boolValue;
                                        
                    NSLog(@"isOpenFence = %d",isOpenFence);
                    
                    NSLog(@"转换的字典:%@",dict);
                    if ([dict[@"data"] isKindOfClass:[NSDictionary class]] && isOpenFence) {
                        
                        YSBWebSocketPushMsgModel *model = [YSBWebSocketPushMsgModel yy_modelWithJSON:dict[@"data"]];
                        [self judgeShowAlertWithModel:model];
                        
                    }
 
                }else{
                    
                    if([self judgeClassToDoAction]) {
                        if (self.delegate && [self.delegate respondsToSelector:@selector(webSocketDidReceiveMessage:)]) {
                            [self.delegate webSocketDidReceiveMessage:message];
                        }
                    }

                }
            }
            
        }
        
    }

}


@end



@implementation YSBHomeController

//改变订单状态
- (void)changeOrderStatus:(NSNotification *)no {
    
    NSLog(@"进入了通知222,通知名:%@",no.name);
    if ([no.name isEqualToString:@"comeFromHomeChangeOrderStatusNotification"]) {//来自首页
        self.take_type = @"normal";
    }else if ([no.name isEqualToString:@"comeFromCollectionChangeOrderStatusNotification"]) {//来自收藏
        self.take_type = @"collect";
    }else if ([no.name isEqualToString:@"comeFromAcceptableChangeOrderStatusNotification"]) {//来自可接订单
        self.take_type = @"myboss";
    }else if ([no.name isEqualToString:@"comeFromInvitationChangeOrderStatusNotification"]) {//来自人才库邀请
        self.take_type = @"invite";
    }else if ([no.name isEqualToString:@"comeFromGoodOrderOfHomeNotification"]) {//来自每日优选首页入口
        self.take_type = @"hpush";
    }else if ([no.name isEqualToString:@"comeFromGoodOrderOfMessageNotification"]) {//来自每日优选消息入口
        self.take_type = @"mpush";
    }else if ([no.name isEqualToString:@"comeFromShareNotification"]) {//来自分享入口
        self.take_type = @"share";
    }else if ([no.name isEqualToString:@"comeFromMessageChangeOrderStatusNotification"]) {//来自通知或消息列表进入详情
        self.take_type = @"mpush";
    }
    
    if ([YSBCommonTool isBlankString:self.take_type]) {
        self.take_type = @"normal";
    }
    
    
    if (![YSBCommonTool isBlankString:no.userInfo[@"oid"]]) {
        NSString *oidStr = no.userInfo[@"oid"];
        self.oid = oidStr;
    }
    
    if ([no.object isKindOfClass:[YSBChildOrdelDetailModel class]]) {
        YSBChildOrdelDetailModel *ordelDetailModel = no.object;
        self.ordelDetailModel = ordelDetailModel;
        if (![YSBCommonTool isBlankString:ordelDetailModel.time_type]) {
            self.timeType = ordelDetailModel.time_type;
        }
    }

    
    NSString *event = @"";
    if ([no.userInfo[@"event"] isKindOfClass:[NSString class]]) {
        event = no.userInfo[@"event"];
    }
    
    self.isNoticeEvent = YES;

    if ([event isEqualToString:@"go"]) {//立即抢单
        [self goAction];
        
    }
    if ([event isEqualToString:@"next"]) {//下一个
        [self changeAction];
    }
    if ([event isEqualToString:@"cancel"]) {//取消匹配
        [self cancelAction];
    }
    
    
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    CGFloat showTime = 2.5f;
    
    NSNumber *isCloseHomeActivityNum = [localSave objectForKey:@"closeHomeActivityKey"];
    BOOL isCloseHomeActivity = isCloseHomeActivityNum.boolValue;
    if (isCloseHomeActivity == false) {
        showTime = 0;
    }

    [self performSelector:@selector(delayShowAdMethod) withObject:nil afterDelay:showTime];
    
}

- (void)delayShowAdMethod {
    if ([YSBMSAlertManager shareManager]) {
        [[YSBMSAlertManager shareManager] showAlerts];
    }
}


@end



@implementation YSBAccountStatusViewController


- (void)noticeStatus:(NSNotification *)no {
    
    NSLog(@"进入了通知333,通知名:%@",no.name);
    if ([no.name isEqualToString:@"comeFromHomeChangeOrderStatusNotification"]) {//来自首页
        self.take_type = @"normal";
    }else if ([no.name isEqualToString:@"comeFromCollectionChangeOrderStatusNotification"]) {//来自收藏
        self.take_type = @"collect";
    }else if ([no.name isEqualToString:@"comeFromAcceptableChangeOrderStatusNotification"]) {//来自可接订单
        self.take_type = @"myboss";
    }else if ([no.name isEqualToString:@"comeFromInvitationChangeOrderStatusNotification"]) {//来自人才库邀请
        self.take_type = @"invite";
    }else if ([no.name isEqualToString:@"comeFromGoodOrderOfHomeNotification"]) {//来自每日优选首页入口
        self.take_type = @"hpush";
    }else if ([no.name isEqualToString:@"comeFromGoodOrderOfMessageNotification"]) {//来自每日优选消息入口
        self.take_type = @"mpush";
    }else if ([no.name isEqualToString:@"comeFromShareNotification"]) {//来自分享入口
        self.take_type = @"share";
    }else if ([no.name isEqualToString:@"comeFromMessageChangeOrderStatusNotification"]) {//来自通知或消息列表进入详情
        self.take_type = @"mpush";
    }
    
    if ([YSBCommonTool isBlankString:self.take_type]) {
        self.take_type = @"normal";
    }
    
    
    if (![YSBCommonTool isBlankString:no.userInfo[@"oid"]]) {
        NSString *oidStr = no.userInfo[@"oid"];
        self.oid = oidStr;
    }
    
    if ([no.object isKindOfClass:[YSBChildOrdelDetailModel class]]) {
        YSBChildOrdelDetailModel *ordelDetailModel = no.object;
        self.ordelDetailModel = ordelDetailModel;
        if (![YSBCommonTool isBlankString:ordelDetailModel.time_type]) {
            self.timeType = ordelDetailModel.time_type;
        }
    }

    
    NSString *event = @"";
    if ([no.userInfo[@"event"] isKindOfClass:[NSString class]]) {
        event = no.userInfo[@"event"];
    }
    
    self.isNoticeEvent = YES;

    if ([event isEqualToString:@"go"]) {//立即抢单
        [self goAction];
        
    }
    if ([event isEqualToString:@"next"]) {//下一个
        [self changeAction];
    }
    if ([event isEqualToString:@"cancel"]) {//取消匹配
        [self cancelAction];
    }
    
    
}


@end



@implementation YSBChildOrderDetailComplaintViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.orderMoneyTitleTextField.textField.keyboardType = UIKeyboardTypeDecimalPad;
    
}


@end



@implementation YSBPunchCardDetailViewController

- (void)securityFenceWithLatitude:(NSString *)latitude andLongitude:(NSString *)longitude {

    
}

- (void)judgeContinuousOrderWithData:(id)responseObject {
    

    
}


@end



@implementation YSBWebViewController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    
    
}


@end



@implementation YSBAnnouncementScrollView


- (void)backGoBtnWithModel:(YSBAnnouncementModel *)model {
    
    if (self.clickBottomBtnBlock) {
        self.clickBottomBtnBlock(model);
    }
    NKAlertView *alertView = (NKAlertView *)self.superview;
    [alertView viewHidden];
    
    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    [localSave setObject:[NSNumber numberWithBool:NO] forKey:@"closeHomeActivityKey"];
    [localSave synchronize];
    
}


- (void)bottomTouchTap:(UITapGestureRecognizer *)sender{
    
    
    self.closeImgView.hidden = YES;
    self.btnInfoLabel.hidden = YES;

    if (self.clickClosedBtnBlock) {
        self.clickClosedBtnBlock();
    }
    [self.main removeFromSuperview];
    NKAlertView *alertView = (NKAlertView *)self.superview;
    [alertView hide];
    
    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    [localSave setObject:[NSNumber numberWithBool:YES] forKey:@"closeHomeActivityKey"];
    [localSave synchronize];
    
}



@end
